﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace WebApplication1.Controllers
{
    public class AuthorcationController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 基础授权
        /// </summary>
        /// <returns></returns>
        public IActionResult Login()
        {
            //判断账号和密码
            //1.创建身份详情
            var claims = new List<Claim>
            {
               new Claim (ClaimTypes.Name,"zhangsan"),
               new Claim ("card","6105211991107413"),
               new Claim ("sex","男")

            };
            //2.身份证 
            var identity = new ClaimsIdentity(claims, "斯沃特");
            //3.登录
            HttpContext.SignInAsync(new ClaimsPrincipal(identity ));
            return View();
        }
        public IActionResult LoginOut()
        {
            HttpContext.SignOutAsync();
            return View();
        }
        /// <summary>
        /// 角色的授权
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public IActionResult LoginRole(string role)
        {
            //判断账号和密码
            //1.创建身份详情 
            var claims = new List<Claim>
            {
               new Claim (ClaimTypes.Name,"zhangsan"),
               new Claim ("card","6105211991107413"),
               new Claim ("sex","男"),
               new Claim (ClaimTypes.Role,role)
            };
            //2.身份证 
            var identity = new ClaimsIdentity(claims, "User_role");
            //3.登录
            HttpContext.SignInAsync(new ClaimsPrincipal(identity));
            return View();
        }
        //基于策略的授权 在start.up里注入 
    }
}
